<template>
  <div>
    <van-nav-bar
      title="地址"
      left-text="返回"
      right-text="保存"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      @select="select"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>

<script>
import $axios from "@/api/request";
export default {
  data() {
    return {
      chosenAddressId: "1",
      addressId: {},
      list: [
        {
          address: "",
        },
      ],
    };
  },
  created() {
    this.address();
  },
  methods: {
    onClickLeft() {
      this.$router.push({
        path: "/order",
        query: { preOrderId: this.$route.query.preOrderId,total:this.$route.query.total},
      });
    },
    onClickRight() {
      this.$router.back();
    },
    onAdd() {
      this.$router.push({
        path: "/AddressEdit",
        query: { preOrderId: this.$route.query.preOrderId,total:this.$route.query.total},
      });
    },
    onEdit(index, item) {
      this.$router.push({
        path: "/Edit",
        query: {
          addressId: index.addressId,
          total:this.$route.query.total,
          preOrderId: this.$route.query.preOrderId
        },
      });
    },
    address() {
      $axios.get("http://zl.huruqing.cn:3003/address/all").then((res) => {
        this.list = res.list;
        this.list.forEach((item, index) => {
          item.id = index;
          item.address = item.addressDetail;
        });
      });
    },
    select(item) {
      this.$store.commit("set_address", item);
    },
  },
};
</script>

<style>
</style>